﻿#region
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using EndlessDownloader.Properties;

#endregion

namespace EndlessDownloader
{
    public partial class ImageNavigator : UserControl
    {
        public ImageNavigator()
        {
            InitializeComponent();

            ptbImage.Image = null;
            lblCount.Text = string.Empty;
        }

        private readonly Dictionary<int, string> _images = new Dictionary<int, string>();
        
        private bool _firstAdded;

        public void AddToMaximum(int downloadIndex, string savedPath)
        {
            hsbScroll.Maximum++;

            _images.Add(downloadIndex, savedPath);

            if (_firstAdded == false)
            {
                hsbScroll.Value = 1;
                hsbScroll.Minimum = 1;

                _firstAdded = true;
            }

            if (Settings.Default.LatestImageVisible)
                hsbScroll.Value = hsbScroll.Maximum;
            else
                RefreshCount();
        }

        private void hsbScroll_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ptbImage.Image = new Bitmap(_images[hsbScroll.Value]);
            }
            catch
            {
                ptbImage.Image = null;
            }

            RefreshCount();
        }

        private void RefreshCount()
        {
            lblCount.Text = string.Format("{0:N0} / {1:N0}", hsbScroll.Value, hsbScroll.Maximum);
        }
    }
}